﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HCY.CTP.DBShangZheng;
using IFServerDLL.DB;

namespace IFServerDLL.Model
{
    
    public class TradeOnHand
    {
        public TradeOnHand()
        {
            _OnhandID = Guid.NewGuid();
        }
        public TradeOnHand(TradeBase trade)
        {
            _OnhandID = Guid.NewGuid();
            _Instrument = trade.Instrument;
            _BuyOrSell = trade.BuyOrSell;
            _AvgOpenPrice = trade.Price;
            _SumTodayVolume = trade.TodayVolume;
            _SumYesterDayVolume = trade.YesterdayVolume;
            _ListTrade.Add(trade);
            _SumCash = trade.Cash;
            _SumCommission = trade.Commission;
            _CloseProfit = 0;
            _OnHandProfit = 0;
        }
        private Guid _OnhandID;
        public Guid OnhandID { get { return _OnhandID; } }

        private string _Instrument;
        public string Instrument
        {
            get { return _Instrument; }
        }
        private MyEnumDirection _BuyOrSell;
        public MyEnumDirection BuyOrSell
        {
            get { return _BuyOrSell; }
        }

        List<TradeBase> _ListTrade = new List<TradeBase>();
        public List<TradeBase> ListTrade
        {
            get { return _ListTrade; }
        }
        
        decimal _SumCash;
        public decimal SumCash
        {
            get { return _SumCash; }
        }
        decimal _SumCommission;
        public decimal SumCommission
        {
            get { return _SumCommission; }
        }
        decimal _OnHandProfit;
        public decimal MarketProfit
        {
            get { return _OnHandProfit; }
        }

        public void UpdateMarketData(MyDBData myif)
        {
            if (myif.Instrument == _Instrument)
            {
                if (_BuyOrSell == MyEnumDirection.Buy)
                {
                    _OnHandProfit = (myif.NewPrice - _AvgOpenPrice) * SumALLVolume*MyVariety.GetVarietyByInstrument(_Instrument).Count;
                }
                else
                {
                    _OnHandProfit = (_AvgOpenPrice - myif.NewPrice) * SumALLVolume * MyVariety.GetVarietyByInstrument(_Instrument).Count;
                }
            }
        }

        public void UpdateTrade(TradeBase trade)
        {
            _OnhandID = Guid.NewGuid();
            _ListTrade.Add(trade);
            _AvgOpenPrice = _ListTrade.Where(p => p.ALLVolumne > 0).Average(p => p.Price as decimal?) ?? 0;
            _AvgClosePrice = _ListTrade.Where(p => p.ALLVolumne < 0).Average(p => p.Price as decimal?)??0;
            _SumYesterDayVolume = _ListTrade.Sum(p => p.YesterdayVolume as int?) ?? 0;
            _SumTodayVolume = _ListTrade.Sum(p => p.TodayVolume as int?) ?? 0;
            _SumCash = _ListTrade.Sum(p => p.Cash as decimal?) ?? 0;
            _SumCommission = _ListTrade.Sum(p => p.Commission as decimal?) ?? 0;
            if (BuyOrSell == MyEnumDirection.Buy)
            {
                _CloseProfit = (_AvgClosePrice - _AvgOpenPrice) * (_ListTrade.Where(p => p.ALLVolumne < 0).Sum(p => p.ALLVolumne as int?) ?? 0) *(-1)*MyVariety.GetVarietyByInstrument(_Instrument).Count;
            }
            else
            {
                _CloseProfit = (_AvgOpenPrice - _AvgClosePrice) * (_ListTrade.Where(p => p.ALLVolumne < 0).Sum(p => p.ALLVolumne as int?) ?? 0) * (-1) * MyVariety.GetVarietyByInstrument(_Instrument).Count;
            }
        }



        private int _SumYesterDayVolume;
        public int SumYesterDayVolume
        {
            get { return _SumYesterDayVolume; }
        }
        private int _SumTodayVolume;
        public int SumTodayVolume
        {
            get { return _SumTodayVolume; }
        }

        public int SumALLVolume
        {
            get { return _SumTodayVolume+_SumYesterDayVolume; }
        }

        private decimal _AvgClosePrice;
        public decimal AvgClosePrice
        {
            get { return _AvgClosePrice; }
        }
        private decimal _CloseProfit;
        public decimal CloseProfit
        {
            get { return _CloseProfit; }
        }

        private decimal _AvgOpenPrice;
        public decimal AvgOpenPrice
        {
            get { return _AvgOpenPrice; }
        }
    }
}
